package com.zhengsr.latte.ui.recycler.adapter.headroot;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.zhengsr.latte.ui.recycler.adapter.base.RBaseViewholder;


/**
 * Created by zhengshaorui on 2018/5/14.
 */

public class HeaderFooterAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "HeaderFooterAdapter";
    //防止冲突，值设置大一点
    private static final int BASE_HEADER_ITEM_INDEX = 10000;
    private static final int BASE_FOOTER_ITEM_INDEX = 20000;

    private SparseArrayCompat<View> mHeaderView = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFooterView = new SparseArrayCompat<>();

    private RecyclerView.Adapter mInnerAdapter; //最开始我们的adapter，在原来的基础上，添加header和footer
    public HeaderFooterAdapter(RecyclerView.Adapter adapter) {
        mInnerAdapter = adapter;
    }

    public void addHeaderView(View view){
        mHeaderView.put(mHeaderView.size() + BASE_HEADER_ITEM_INDEX,view);
    }
    public void addFooterView(View view){
        mFooterView.put(mFooterView.size() + BASE_FOOTER_ITEM_INDEX,view);
    }

    private boolean isHeaderView(int position){
        return position < mHeaderView.size();
    }

    private boolean isFootView(int position){
        return position >= mInnerAdapter.getItemCount() + mHeaderView.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //如果是头部和底部，则返回他们的 viewholder
        if (mHeaderView.get(viewType) != null){
            return RBaseViewholder.getViewHolder(parent.getContext(),mHeaderView.get(viewType));
        }else if(mFooterView.get(viewType) != null) {
            return RBaseViewholder.getViewHolder(parent.getContext(), mFooterView.get(viewType));
        }
        //否则返回自己的
        return mInnerAdapter.onCreateViewHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //头部和底部，我们并不需要绑定view，直接return即可
        if (isHeaderView(position)){
            return;
        }else if(isFootView(position)){
            return ;
        }
        mInnerAdapter.onBindViewHolder(holder,position - mHeaderView.size());
    }

    @Override
    public int getItemCount() {
        //个数返回，直接返回所有
        return mHeaderView.size()+mFooterView.size()+mInnerAdapter.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)){
            return mHeaderView.keyAt(position);
        }else if(isFootView(position)){
            //如果是 footer，key 为 position - mHeaderView.size() - mInnerAdapter.getItemCount()
            return mFooterView.keyAt(position - mHeaderView.size() - mInnerAdapter.getItemCount());
        }
        return mInnerAdapter.getItemViewType(position - mHeaderView.size());
    }

    /**
     * gridview需要在这里做一下特殊处理
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mInnerAdapter.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        //如果是 gridview
        if (layoutManager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spancount = gridLayoutManager.getSpanSizeLookup();
            //可以理解为合并单元格的意思，现在的 spancount 为2，如果 getspansize 返回也为2，则合并了这个 item
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int viewtype = getItemViewType(position);
                    if (mHeaderView.get(viewtype) != null){
                        return gridLayoutManager.getSpanCount();
                    }else if(mFooterView.get(viewtype) != null){
                        return gridLayoutManager.getSpanCount();
                    }
                    if (spancount != null){
                        return spancount.getSpanSize(position);
                    }
                    return 1;
                }
            });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
    }

    //处理 staggeredgridlayoutmanager的情况
    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        mInnerAdapter.onViewAttachedToWindow(holder);
        int position = holder.getLayoutPosition();
        if (isHeaderView(position) || isFootView(position)){
            ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
            if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams){
                ((StaggeredGridLayoutManager.LayoutParams) lp).setFullSpan(true);
            }
        }
    }
}
